package com.qxl.forest.business.persistence.dao;

import com.qxl.forest.business.persistence.entity.Menu;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: qiuxinlin
 * @Dercription:
 * @Date: 13:56 2017/11/24
 */
@Repository
public interface MenuDao extends PagingAndSortingRepository<Menu, Integer> {
    /**
     * 根据角色id获取菜单url
     *
     * @param roleId
     * @return
     */
    @Query(value = "select url from f_relation rel inner join f_menu m on rel.menuid = m.id where rel.roleid = ?1", nativeQuery = true)
    List getResUrlsByRoleId(Integer roleId);

    /**
     * 根据角色列表获取菜单列表
     *
     * @param roleIds
     * @return
     */
    @Query(value = "SELECT * FROM f_menu m1 WHERE m1.id IN (SELECT menuid FROM f_relation rela WHERE rela.roleid IN ( ?1 )) AND m1.ismenu =1 ORDER BY levels,num ASC", nativeQuery = true)
    List<Menu> getMenusByRoleIds(String roleIds);

    /**
     * 根据编码code查找菜单
     *
     * @param code
     * @return
     */
    Menu findByCode(String code);

    /**
     * 查找当前菜单的所有子菜单
     *
     * @return
     */
    List findByPcodesLike(String code);

    /**
     * 通过pid查找menu
     *
     * @param pid
     * @return
     */
    List findByPid(Integer pid);
}
